const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const path = require('path');
const session = require('express-session');
const FileStore = require('session-file-store')(session);
// 1. 引入模板 中间件
// path 路径模块, join 返回有效路径 
// __dirname 当前项目的物理路径, 
// views ./views 在当前项目的根上 
// MVC View  views 
// console.log(path.join(__dirname, 'views'));
app.set('views', path.join(__dirname, 'views')); // 设置view路径
app.set('view engine', 'ejs');

// 启用一个body-parser
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
app.use(session({
  name:'skey',
  secret:'dkjgdkjfk',
  store:new FileStore(),
  saveUninitialized:false,  // 是否保存未
  resave:false,
  cookie:{
    maxAge:1000*1000
  }
}))

// controller 
app.get('/', (req, res) => {
  // view 
  // cookie
  var sesson=req.session;
  var loginUser=sesson.loginUser;
  var isLogined=!!loginUser;
  console.log(sesson,'.......');
  if(isLogined){
    res.render('index',{
      title:'首页，搞',
      isLogined:true,
      loginUser:loginUser
    })
  }
  else res.render('index', {
    title: '首页，咏琪跳高破纪录...',
    isLogined: false,
  })
})

app.get('/logout',(req,res)=>{
  req.session.destroy((err)=>{
    res.clearCookie('skey');
    res.redirect('./');
  })
})

app.post('/login', function(req, res) {
  // name password? 
  console.log(req.body, '-----------------');
  const {name, password} = req.body;
  if(name=='root' && password=='123456'){
    console.log('1');
    req.session.regenerate((err)=>{
      if(err){
        return res.json({
          code:2,
          msg:'登录失败'
        })
      }
      req.session.loginUser=name;
      res.json({
      code:0,
      msg:'登录成功'
    })
    })
  }else{
    res.json({
      code:1,
      msg:'账号密码错误'
    })
  }
})
app.listen(3000, () => {
  console.log('服务器启动起来')
})
